package com.yonyou.crm.base.measure.ctrl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.measure.entity.MeasuresVO;
import com.yonyou.crm.base.measure.rmitf.IMeasuresRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RestController
@RequestMapping(value = "/base/measures")
public class MeasuresCtrl {

	@Autowired
	private IMeasuresRmService service;
	
	/**
	 * 查询计量单位
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<MeasuresVO> measurePage = service.getList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		
		return ResponseUtil.getObject(measurePage);
	}
	
	/**
	 * 查询计量单位参照
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(value = "ref",method = RequestMethod.GET)
	public Object listRef(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		
		if (paramObj != null) {
			//有分页信息返回分页列表
			if (paramObj.containsKey(HttpKeyConstant.Page) && paramObj.containsKey(HttpKeyConstant.PageSize)) {
				return ResponseUtil.getObject(service.getRefPage(getRequestPage(paramObj), null));
			}
		}
		
		List<MeasuresVO> voList = service.getRefList(null);
		
		Page<MeasuresVO> page = new Page<MeasuresVO>();
		page.setData(voList);
		
		return ResponseUtil.getObject(page);
	}

	/**
	 * 计量单位详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}		
		
		return ResponseUtil.getObject(service.getDetail(id));
	}
	
	
	/**
	 * 新增计量单位
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		MeasuresVO measure = RequestUtil.getVO(request, MeasuresVO.class);
		
		String validMsg = insertValidator(measure);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(validMsg);
		}
		
		measure = service.insert(measure);
		
		if (measure.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}
				
		return ResponseUtil.getObject(measure);
	}

	/*
	 * 计量单位新增校验
	 * @param measure
	 * @return String
	 */
	private String insertValidator(MeasuresVO measure) {
		
		if (measure == null) {
			return "转换实体失败";
		}
		
		if (!StringUtils.isNotBlank(measure.getCode())) {
        	return  "计量单位代码不能为空";
		}
		
		if (!StringUtils.isNotBlank(measure.getName())) {
        	return  "计量单位名称不能为空";
		}
		
		return "";
	}
	
	/**
	 * 修改计量单位
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		MeasuresVO measure = RequestUtil.getVO(request, MeasuresVO.class);		
		if (measure == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}
		
		measure.setId(id);
		
		String validMsg = updateValidator(measure);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(validMsg);
		}

		MeasuresVO newMeasure = service.update(measure);
		
		return ResponseUtil.getObject(newMeasure);
	}
	
	/*
	 * 计量单位编辑校验
	 * @param measure
	 * @return String
	 */
	private String updateValidator(MeasuresVO measure) {
		
		if (measure == null) {
			return "转换实体失败";
		}
		
		if (!StringUtils.isNotBlank(measure.getCode())) {
        	return  "计量单位代码不能为空";
		}
		
		if (!StringUtils.isNotBlank(measure.getName())) {
        	return  "计量单位名称不能为空";
		}
		
		return "";
	}
		
	/**
	 * 删除计量单位
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id ,HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}
		
		service.delete(id);
		
		return ResponseUtil.getObject();
	}
	
	/**
	 * 批量删除计量单位
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] idArray = idsStr.split(",");
		
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		
		 String message = service.batchDelete(idArray);
		
		 Page<MeasuresVO> measurePage = service.getList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		 
		return ResponseUtil.getObject(measurePage, message);
	}
		
	/**
	 * 停启用计量单位
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}/state", method = RequestMethod.PUT)
	public Object updateEnableState(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}

		Integer enableState = (Integer)RequestUtil.getObject(request, "enableState");
				
		if (enableState == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}

		return service.updateEnableState(id, enableState);
	}
	
	/**
	 * 批量停启用计量单位
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] idArray = idsStr.split(",");
		
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		
		Integer enableState = paramObj.getInt("enableState");
		
		if (enableState == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}
		
		Page<MeasuresVO> measurePage = service.batchUpdateEnableState(idArray, enableState, getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
				
		return ResponseUtil.getObject(measurePage);
	}
	
	/**
	 * 获取分页信息
	 * @param paramObj
	 * @return Page<MeasuresVO>
	 */
	private Page<MeasuresVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		
		return new Page<MeasuresVO>(page, pageSize);
	}
	
	
}
